package com.enfi.api.util;

import com.google.common.collect.Maps;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class SecUser implements UserDetails {
    private static final long serialVersionUID = 1L;
    private final String id;
    //帐号,
    private final String username;
    //密码
    private final String password;

    //角色集合
    private final Collection<? extends GrantedAuthority> authorities;

    private  Map data;

    private boolean accountNonExpired = true;
    private boolean accountNonLocked = true;
    private boolean credentialsNonExpired = true;
    private boolean enabled = true;

    public SecUser(String id, String username, String password, Collection<? extends GrantedAuthority> authorities) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.authorities = authorities;
        this.data = Maps.newHashMap();
    }

    public SecUser(String id, String username, String password, Collection<? extends GrantedAuthority> authorities, boolean accountNonExpired,
                   boolean accountNonLocke, boolean credentialsNonExpired, boolean enabled
    ) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.authorities = authorities;
        this.data = Maps.newHashMap();
        this.accountNonExpired = accountNonExpired;
        this.accountNonLocked = accountNonLocke;
        this.credentialsNonExpired = credentialsNonExpired;
        this.enabled = enabled;
    }

    public SecUser(String id, String username, String password, Collection<? extends GrantedAuthority> authorities, Map data) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.authorities = authorities;
        this.data = data;
    }

    public void setData(Map data) {
        this.data = data;
    }

    public Object getValueOfData(String key){
        if(this.data!=null){
           return this.data.get(key);
        }
        return null;
    }

    public Map getData() {
        return data;
    }

    //返回分配给用户的角色列表
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    public List<String> getAuthoritiesToList(){
        return authorities.stream().map(v->v.getAuthority()).collect(Collectors.toList());
    }

    public String getId() {
        return id;
    }

    @Override
    public String getPassword() {
        return password;
    }

    //虽然我数据库里的字段是 `account`  ,这里还是要写成 `getUsername()`,因为是继承的接口
    @Override
    public String getUsername() {
        return username;
    }

    // 账户是否未过期
    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    // 账户是否未锁定
    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    // 密码是否未过期
    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    // 账户是否激活
    @Override
    public boolean isEnabled() {
        return enabled;
    }
}